package com.dmgis.hndz.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.dmgis.hndz.pojo.Qxab02b;
import com.dmgis.hndz.pojo.Qxab03a;
import com.dmgis.hndz.pojo.Qxbb01a;
import com.dmgis.hndz.pojo.Ybba01D;
import com.dmgis.hndz.pojoMongo.*;
import com.dmgis.hndz.pojoVo.DynamicWaringShortVo;
import com.dmgis.hndz.pojoVo.TwoStringVo;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Sorts;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SkPngFileService {
    @Value("${download.directory}")
    private String downloadDirectory;

    @Qualifier("mongoPrimaryTemplate")
    @Autowired
    private MongoTemplate mongoTemplate;

    @Qualifier("mongoPrimaryTemplate")
    @Autowired
    private MongoTemplate mongoSecondTemplate;

    public SkPngFile getImageByTime(String datetime) {
        return mongoTemplate.findOne(new Query(
                Criteria.where("datetime").is(datetime)), SkPngFile.class);
    }

    public DlPngFile getDlPngFile(String file_id) {
        return mongoTemplate.findOne(new Query(
                Criteria.where("file_id").is(file_id)
        ), DlPngFile.class);
    }

    public List<SkPngFile> findPngFilesByDatetime(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        String dateTime = jsonObject.getString("dateTime");

//        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
//        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, inputFormatter);
//
//        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
//        String formattedDate = localDateTime.format(outputFormatter);

        Criteria criteria = Criteria.where("datetime").regex("^" + dateTime);
        Query query = new Query(criteria);

        return mongoTemplate.find(query, SkPngFile.class);
    }

    public List<YbPngFile> findForecastPngFilesByDatetimePg(String dateTime) {

//        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
//        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, inputFormatter);
//
//        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
//        String formattedDate = localDateTime.format(outputFormatter);

        Criteria criteria = Criteria.where("datetime").regex("^" + dateTime);
        Query query = new Query(criteria);

        return mongoTemplate.find(query, YbPngFile.class);
    }

    public List<YbPngFile> findForecastPngFilesByDatetimeMg(String dateTime, String nTime) {
        Query query = new Query();
        if ("8".equals(nTime)) {
            Criteria criteria = Criteria.where("datetime")
                    .regex(dateTime+" 08:00:00");
            query = new Query(criteria);
            return mongoTemplate.find(query, YbPngFile.class);
        } else if ("20".equals(nTime)) {
            Criteria criteria = Criteria.where("datetime")
                    .regex(dateTime+" 20:00:00");
            query = new Query(criteria);
            return mongoTemplate.find(query, YbPngFile.class);
        }
        return new ArrayList<>();
    }

    public List<ImageRaderPngFile> findRadarChatByFileId(List<Qxab02b> list) {
        List<ImageRaderPngFile> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String file_id = list.get(i).getQxab02b100();

            ImageRaderPngFile mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
                    Criteria.where("file_id").is(file_id)
            ), ImageRaderPngFile.class);

            resultList.add(mongoTemplateOne);
        }
        return resultList;
    }

    public List<ImageCloudPngFile> findCloudChatByFileId(List<Qxab03a> list) {
        List<ImageCloudPngFile> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String file_id = list.get(i).getQxab03a100();

            ImageCloudPngFile mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
                    Criteria.where("file_id").is(file_id)
            ), ImageCloudPngFile.class);

            resultList.add(mongoTemplateOne);
        }
        return resultList;
    }

    public List<PdfServiceProduct> findServiceProductByTableId(List<Qxbb01a> list) {
        List<PdfServiceProduct> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String file_id = list.get(i).getQxbb01a060();

            PdfServiceProduct mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
                    Criteria.where("file_id").is(file_id)
            ), PdfServiceProduct.class);

            resultList.add(mongoTemplateOne);
        }
        return resultList;
    }

    //    public List<ShortWarnProductJpgFile> findDynamicNearDisasterWaringImageByTableId(List<Ybba01D> list) {
//        List<ShortWarnProductJpgFile> resultList = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            String table_id = list.get(i).getYbba01d060();
//
//            ShortWarnProductJpgFile mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
//                    Criteria.where("table_id").is(table_id)
//            ), ShortWarnProductJpgFile.class);
//
//            resultList.add(mongoTemplateOne);
//        }
//        return resultList;
//    }
    public List<DlPngFile> findDynamicNearDisasterWaringImageByTableId(List<Ybba01D> list) {
        List<DlPngFile> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String file_id = list.get(i).getYbba01d060();

            DlPngFile mongoTemplateOne = mongoTemplate.findOne(new Query(
                    Criteria.where("file_id").is(file_id)
            ), DlPngFile.class);

            resultList.add(mongoTemplateOne);
        }
        return resultList;
    }

    public ShortWarnProductJpgFile findTodayProvinceShortWaringByTableId(DynamicWaringShortVo dynamicWaringShortVo) {
        String table_id = dynamicWaringShortVo.getYbba01a010();
        ShortWarnProductJpgFile mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
                Criteria.where("table_id").is(table_id)
        ), ShortWarnProductJpgFile.class);
        return mongoTemplateOne;
    }

    public DocWarnProductCityFile findTodayProvinceShortWaringDocByTableId(DynamicWaringShortVo dynamicWaringShortVo) {
        String table_id = dynamicWaringShortVo.getYbba01a010();
        DocWarnProductCityFile mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
                Criteria.where("table_id").is(table_id)
        ), DocWarnProductCityFile.class);
        return mongoTemplateOne;
    }

    public List<MidShortProductJpgFile> findTodayProvinceMidShortWaringByTableId(List<TwoStringVo> list) {
        List<MidShortProductJpgFile> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String table_id = list.get(i).getString1();

            MidShortProductJpgFile mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
                    Criteria.where("table_id").is(table_id)
            ), MidShortProductJpgFile.class);

            resultList.add(mongoTemplateOne);
        }
        return resultList;
    }

    public List<DocWarnProductCityFile> findTodayProvinceMidShortWaringDocByTableId(List<TwoStringVo> list) {
        List<DocWarnProductCityFile> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String table_id = list.get(i).getString1();

            DocWarnProductCityFile mongoTemplateOne = mongoSecondTemplate.findOne(new Query(
                    Criteria.where("table_id").is(table_id)
            ), DocWarnProductCityFile.class);

            resultList.add(mongoTemplateOne);
        }
        return resultList;
    }

    public List<SkPngFile> findPngFilesByDatetimePg(String dateTime) {

        Criteria criteria = Criteria.where("datetime").regex("^" + dateTime);
        Query query = new Query(criteria);

        return mongoTemplate.find(query, SkPngFile.class);

    }

    public List<SkPngFile> findFilesByDatetimeMg(String dateTime) {
        Criteria criteria = Criteria.where("datetime")
                .regex("^" + dateTime);
        Query query = new Query(criteria);
        //添加排序功能
        query.with(Sort.by(Sort.Direction.DESC, "ntime"));
        // 执行查询并添加排序
        return mongoTemplate.find(query, SkPngFile.class);
    }
}
